package cn.learn.office.excel_v3.read;

import cn.learn.office.excel_v3.ConvertFieldName;
import cn.learn.office.excel_v3.IGetter;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huangyezhan
 * @version 1.0
 * @date 2021年06月12日16:37
 */

@Getter
@RequiredArgsConstructor
public class ReadHeader {

    private final List<HeaderCell> list;


    public static HeaderCell headerCellBuilder() {
        return new HeaderCell();
    }

    @Accessors(fluent = true)
    @NoArgsConstructor
    public static class HeaderCell {
        private final List<HeaderCell> list = new ArrayList<>();

        public HeaderCell(int colIndex, String name) {
            this.colIndex = colIndex;
            this.name = name;
        }

        /**
         * 列的索引号
         */
        @Getter
        @Setter
        private int    colIndex;
        /**
         * 对应的字段名称
         */
        @Getter
        private String name;

        public HeaderCell name(IGetter<T> name) {
            this.name = ConvertFieldName.convertToFieldName(name);
            return this;
        }

        /**
         * 设置简单信息
         * 只能每个单元格，逐个顺序获取
         *
         * @param keys 写入的键名
         */
        public void setHeader(IGetter<T>[] keys) {
            for (int i = 0; i < keys.length; i++) {
                this.commitCell(i, keys[i]);
            }
        }

        /**
         * 设置每个需要从单元格里面获取的信息
         */
        public void commitCell() {
            this.list.add(new HeaderCell(colIndex, name));
        }

        public void commitCell(int colIndex, IGetter<T> name) {
            this.list.add(
                    new HeaderCell()
                            .colIndex(colIndex)
                            .name(name)
            );
        }

        public ReadHeader build() {
            return new ReadHeader(list);
        }

    }


}
